package yc.star.system.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yc.star.common.controller.BaseController;
import yc.star.common.domain.AppResult;
import yc.star.common.domain.TableDataInfo;
import yc.star.system.domain.exam.dto.ExamAddDTO;
import yc.star.system.domain.exam.dto.ExamEditDTO;
import yc.star.system.domain.exam.dto.ExamQueryDTO;
import yc.star.system.domain.exam.dto.ExamQuestionAddDTO;
import yc.star.system.domain.exam.vo.ExamDetailVO;
import yc.star.system.domain.exam.vo.ExamVO;
import yc.star.system.service.IExamService;

import java.util.List;

@RequestMapping("/exam")
@Tag(name = "竞赛接口")
@RestController
public class ExamController extends BaseController {

    @Autowired
    private IExamService examService;

    @GetMapping("/list")
    public TableDataInfo list (ExamQueryDTO examQueryDTO) {
        List<ExamVO> examVOList  = examService.list(examQueryDTO);
        return getTableDataInfo(examVOList);
    }

    @PostMapping("/add")
    public AppResult<String> add (@RequestBody ExamAddDTO examAddDTO) {
        long examId = examService.add(examAddDTO);
        return AppResult.success(Long.toString(examId));
    }

    @PostMapping("/question/add")
    public AppResult<Void> questionAdd (@RequestBody ExamQuestionAddDTO examQuestionAddDTO) {
        boolean isSave = examService.questionAdd(examQuestionAddDTO);
        return toAppResult(isSave);
    }

    @GetMapping("/detail")
    public AppResult<ExamDetailVO> detail (@RequestParam("examId") @NonNull Long examId) {
        return AppResult.success(examService.detail(examId));
    }

    @PutMapping("/edit")
    public AppResult<Void> edit (@RequestBody ExamEditDTO examEditDTO) {
        int rows = examService.edit(examEditDTO);
        return toAppResult(rows);
    }

    @DeleteMapping("/question/delete")
    public AppResult<Void> questionDelete (Long examId,Long questionId) {
        int rows = examService.questionDelete(examId,questionId);
        return toAppResult(rows);
    }

    @DeleteMapping("/delete")
    public AppResult<Void> delete (Long examId) {
        int rows = examService.delete(examId);
        return toAppResult(rows);
    }

    @PutMapping("/publish")
    public AppResult<Void> publish (Long examId) {
        int rows = examService.publish(examId);
        return toAppResult(rows);
    }

    @PutMapping("/cancelPublish")
    public AppResult<Void> cancelPublish (Long examId) {
        int rows = examService.cancelPublish(examId);
        return toAppResult(rows);
    }

}